package com.leoman.post.dao;

import com.leoman.category.entity.Category;
import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.post.entity.PostBase;
import com.leoman.post.entity.TpPostSub;
import com.leoman.post.entity.ZbPostInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 帖子dao
 * Created by 涂奕恒 on 2016/6/12.
 */
public interface PostBaseDao extends IBaseJpaRepository<PostBase> {

    @Query("select a from PostBase a where a.id = ?1 and a.type = ?2 and a.deleteStatus <> 1")
    PostBase getPostInfo(Long postId, Integer type);

    @Query("select a from PostBase a where (a.content like ?1 or a.name like ?1) and a.deleteStatus <> 1")
    Page<PostBase> iPageDesc(String key, Pageable pageable);

    @Query("select a from Category a where a.name like ?1 and a.type = 1 and a.status = 0")
    Page<Category> pageByType(String name, Pageable pageable);

    @Query("select a from Category a where a.type = 1 and a.status = 0")
    Page<Category> pageByNone(Pageable pageable);

    @Query("select a from PostBase a where a.deleteStatus <> 1 and a.essence = 1")
    Page<PostBase> iPageDesc(Pageable pageable);

    @Query("select a from PostBase a where a.deleteStatus <> 1 and (a.essence = 1 or a.userInfo.id in (select b.sourceUser.id from UserAttention b where b.user.id = ?1))")
    Page<PostBase> iPageDesc2(Long userId, Pageable pageable);

    @SuppressWarnings("unused")
    @Query("select a from PostBase a where a.type = 3 and a.userInfo.id in (select b.sourceUser.id from UserAttention b where b.user.id = ?1)")
    Page<PostBase> iPageAttention(Long userId, Pageable pageable);

    @Query("select a from PostBase a where a.category.id = ?1 and a.deleteStatus <> 1 and a.essence <> 1")
    Page<PostBase> iPageByCategory(Long category, Pageable pageable);

    @Query("select a from PostBase a where a.userInfo.id = ?1 and a.deleteStatus <> 1")
    Page<PostBase> iPageMyPost(Long userId, Pageable pageable);

    @Query("select a from PostBase a where a.category.id = ?1 and a.userInfo.id = ?2 and a.deleteStatus <> 1")
    Page<PostBase> iPageMyPostParams(Long category, Long userId, Pageable pageable);

    @Query("select a from TpPostSub a where a.postBase.id = ?1 and a.postBase.deleteStatus <> 1")
    List<TpPostSub> iFindListTP(Long postId);

    @Query("select a from ZbPostInfo a where a.postBase.id = ?1 and a.postBase.deleteStatus <> 1 order by a.id desc")
    List<ZbPostInfo> iFindListZB(Long postId);

    @Query("select a from PostBase a where a.userInfo.id = ?1 and a.type = 3 and a.deleteStatus <> 1 order by a.id desc")
    List<PostBase> iFindListByUserId(Long userId);

    @Query("select a from PostBase a where a.category.id = ?1 and a.type = ?2 and a.deleteStatus <> 1 order by a.updateDate desc ")
    List<PostBase> findByCategoryId(Long categoryId, Integer type);

    @Query("select a from PostBase a where a.createDate > ?1 and a.type = ?2 and a.deleteStatus <> 1")
    List<PostBase> findListNew(Long oldDate, Integer type);

    @Query("select a from PostBase a where a.type = ?1 and a.deleteStatus <> 1")
    List<PostBase> findByType(Integer type);

    @Query("select a from PostBase a where a.essence = ?1 and a.deleteStatus <> 1")
    List<PostBase> findByEssence(Integer essence);

    @Query("select a from PostBase a where a.deleteStatus = ?1")
    List<PostBase> findByStatus(Integer status);

    @Query("select a from PostBase a where a.id in (select b.sourceId from UserCollect b where b.userInfo.id = ?1 and b.type = 4) and a.deleteStatus <> 1")
    Page<PostBase> iPageByUserCollect(Long userId, Pageable pageable);

    /*@Query("select a from PostBase a where a.category.id = ?1 and a.type <> 3 and a.deleteStatus <> 1 order by a.updateDate desc ")
    public List<PostBase> findByCategoryId(Long categoryId);*/

    //查询用户当天的发帖数量
    @SuppressWarnings("unused")
    @Query("SELECT COUNT(a) FROM PostBase a WHERE a.userInfo.id=?1 AND a.createDate >= ?2 AND a.createDate < ?3 AND a.type = 3 and a.deleteStatus <> 1")
    Integer findByTodayPostNum(Long userId, Long morningDate, Long nightDate);

    @Query("select a from PostBase a where a.deleteStatus <> 1")
    List<PostBase> findByDeleteStatue();

    @Query("select a from PostBase a where a.userInfo.id = ?1 and a.deleteStatus <> 1")
    List<PostBase> findByUserId(Long userId);

    @Query("select a from PostBase a where a.category.id = ?1 and a.deleteStatus <> 1 and a.type <> 3 order by a.updateDate desc ")
    List<PostBase> findListCategoryId(Long categoryId);

    @Query("select a from PostBase a where a.category.id = ?1 and a.deleteStatus <> 1 order by a.updateDate desc ")
    List<PostBase> findListAllByCategoryId(Long categoryId);
}
